package com.cyxy.ojbackendmodel.model.vo;

import cn.hutool.json.JSONUtil;
import com.cyxy.ojbackendmodel.model.dto.question.JudgeCase;
import com.cyxy.ojbackendmodel.model.dto.question.JudgeConfig;
import com.cyxy.ojbackendmodel.model.entity.Question;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.Date;
import java.util.List;


@Data
//题目封装类
public class QuestionVO implements Serializable {
    //id
    private Long id;

    //标题
    private String title;

    //内容
    private String content;

    //答案*
    private String answer;

    //标签列表
    private List<String> tags;

    //输入输出配置（json 数组）
    private JudgeCase judgeCase;

    //判题配置（json 对象）
    private JudgeConfig judgeConfig;

    //创建用户 id
    private Long userId;

    //创建时间
    private Date createTime;

    //更新时间
    private Date updateTime;

    //创建题目人的信息
    private UserVO userVO;

    //包装类转对象
    public static Question voToObj(QuestionVO questionVO) {
        if (questionVO == null) {
            return null;
        }
        Question question = new Question();
        BeanUtils.copyProperties(questionVO, question);
        List<String> tagList = questionVO.getTags();
        if (tagList != null) {
            question.setTags(JSONUtil.toJsonStr(tagList));
        }
        JudgeConfig voJudgeConfig = questionVO.getJudgeConfig();
        if (voJudgeConfig != null) {
            question.setJudgeConfig(JSONUtil.toJsonStr(voJudgeConfig));
        }
        return question;
    }

    //对象转包装类
    public static QuestionVO objToVo(Question question) {
        if (question == null) {
            return null;
        }
        QuestionVO questionVO = new QuestionVO();
        BeanUtils.copyProperties(question, questionVO);
        List<String> tagList = JSONUtil.toList(question.getTags(), String.class);
        questionVO.setTags(tagList);
        String judgeConfigStr = question.getJudgeConfig();
        questionVO.setJudgeConfig(JSONUtil.toBean(judgeConfigStr, JudgeConfig.class));
        return questionVO;
    }

    private static final long serialVersionUID = 1L;
}